package com.sinopec.basemodule;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.text.TextUtils;

import androidx.multidex.MultiDexApplication;

import com.orhanobut.hawk.Hawk;
import com.sinopec.basemodule.common.ConfigManager;
import com.sinopec.basemodule.common.UserManager;
import com.sinopec.basemodule.model.app_model.AppUpdateBean;

//import cn.jpush.android.api.JPushInterface;

/**
 * 创建人：吴冬冬<br/>
 * 创建时间：2019/8/14 17:15 <br/>
 */
public class BaseApplication extends MultiDexApplication {
    private static BaseApplication mApp;
    private String token;

    private UserManager userManager;
    private ConfigManager configManager;

    //是否已经自动展示过升级提示弹窗,手动更新不受其影响
    public static boolean isShowAutoUpdateFlag = false;
    private AppUpdateBean appUpdateBean;

    @Override
    public void onCreate() {
        super.onCreate();
        mApp = this;
    }

    public UserManager getUserManager() {
        if (userManager == null) {
            synchronized (BaseApplication.class) {
                if (userManager == null) {
                    initManager();
                }
            }
        }
        return userManager;
    }

    protected void initManager() {
        userManager = new UserManager();
        configManager = new ConfigManager();
    }

    public AppUpdateBean getAppUpdateBean() {
        if (null == appUpdateBean) {
            appUpdateBean = new AppUpdateBean();
        }
        return appUpdateBean;
    }

    public static BaseApplication getInstance() {
        return mApp;
    }

    public ConfigManager getConfigManager() {
        if (configManager == null) {
            initManager();
        }
        return configManager;
    }


    public String getVersion() {
        PackageManager packageManager = getPackageManager();
        String mVersionName = "";
        try {
            PackageInfo packageInfo = packageManager.getPackageInfo(getPackageName(), 0);
            mVersionName = packageInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return "V" + mVersionName;
    }

    public void exit() {
        getUserManager().setUser(null);
        getConfigManager().setConfigInfo(null);
        updateToken(null);
        //极光推送-调用此 API 来删除别名（手机号码）。
//        JPushInterface.deleteAlias(this, 2);
    }

    public void updateToken(String token) {
        Hawk.delete(AppConstants.HAWK_TOKEN);
        Hawk.put(AppConstants.HAWK_TOKEN, token);
        this.token = token;
    }

    public String getToken() {
        if (TextUtils.isEmpty(token)) {
            token = Hawk.get(AppConstants.HAWK_TOKEN);
        }
        return token == null ? "" : token;
    }


    /**
     * get App versionCode
     */
    public Integer getVersionCode() {
        PackageManager packageManager = getPackageManager();
        PackageInfo packageInfo;
        String versionCode = "";
        try {
            packageInfo = packageManager.getPackageInfo(getPackageName(), 0);
            versionCode = packageInfo.versionCode + "";
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return Integer.valueOf(versionCode);
    }

    /**
     * get App versionName
     */
    public String getVersionName() {
        PackageManager packageManager = getPackageManager();
        PackageInfo packageInfo;
        String versionName = "V ";
        try {
            packageInfo = packageManager.getPackageInfo(getPackageName(), 0);
            versionName += packageInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionName;
    }

    /**
     * 全局调用app更新
     *
     * @param isUserClick 是否是用户手动更新
     */
    public void appUpdateEvent(boolean isUserClick) {

    }
}
